/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mvc.model;

import com.gesturn2.comercio.Servicio;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author MAN
 */
public class Servicio_Dao1 extends Model implements DaoGenerico {

     

    public boolean existe(Object o) throws SQLException{

             Integer id = (Integer) o;
             
		boolean sw=true;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

                 try {
            pstmt = getCon().prepareStatement("SELECT COUNT(*) FROM servicios WHERE codigo =?");
            pstmt.setInt(1, id.intValue());
            rs = pstmt.executeQuery();
             if (rs.next()) {
                if (rs.getString(1).equals("0")) {
                    sw = false;
                }
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }
        System.out.print("Valor sw:" + sw);
        return sw;
    }



      public Object load(ResultSet rs) throws SQLException {

        Servicio u = new Servicio();
        u.setCodigo(rs.getString(1));
        u.setNombre(rs.getString(2));
        u.setValor(rs.getDouble(3));
        u.setDescripcion(rs.getString(4));
        


        return u;
    }


      public Object extrae (Object id) throws SQLException{
          String id1=(String)id;
                    Connection conex=getCon();

                if (!isConnect())
			throw new SQLException("no hay conexcion");
                Servicio u= new Servicio();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT * FROM servicios WHERE codigo=?");
			pstmt.setString(1,id1);
			rs=pstmt.executeQuery();
			if(rs.next()){
			  u=(Servicio)load(rs);

                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return u;
     }
      

        public void crear(Object o)throws SQLException{
         Servicio u=(Servicio)o;
             Connection conex=getCon();
             Integer id = new Integer(u.getCodigo());
                if(existe(id))
                    throw new SQLException("Ya existe esta Servicio");
		PreparedStatement pstmt=null;

		try{

			pstmt=conex.prepareStatement("INSERT INTO Servicios VALUES (?,?,?,?)");

                        pstmt.setString(1, u.getCodigo());
			pstmt.setString(2,u.getNombre());
			pstmt.setDouble(3,u.getValor());
                        pstmt.setString(4,u.getDescripcion());
			

			pstmt.executeUpdate();
                    
		}finally{
			if(pstmt!=null){
				pstmt.close();
				pstmt=null;
			}
		}
     
  }



       public List listar() throws SQLException {
              Connection conex=getCon();
        if (!isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Usuario u = null;
        List lista = new LinkedList();
        try {
            ps =(PreparedStatement)  conex.prepareStatement("select * from servicios ");
            rs = ps.executeQuery();

            while (rs.next()) {
                u =(Usuario) load(rs);
                lista.add(u);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

      

    public Object extrae2(Object id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listar(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   public void Update(Object o) throws SQLException {
       Servicio serv=(Servicio)o;
        Connection conex=getCon();
        if (!isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
         try {
            ps =(PreparedStatement)  conex.prepareStatement("UPDATE servicios SET nombre=?, valor=?,descripcion=? where servicios.codigo=? ");
             ps.setString(1, serv.getNombre());
             ps.setDouble(2, serv.getValor());
             ps.setString(3, serv.getDescripcion());
             ps.setString(4, serv.getCodigo());
            rs = ps.executeQuery();

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
      
    }





}
